<?php
	class Frame
	{
		public static function run()
		{
			//1.定义基础目录常量
			static::initConst();
			//初始化配置参数
			static::initConfig();
			//2.确定分发参数
			static::initDispatchParam();
			//3.确定平台相关常量
			static::initPlatformConst();
			//4.注册自动加载
			static::initAutoload();
			//请求分发
			static::initDispatch();
		}
		private static function initConst()
		{
			//根目录常量
			define('ROOT_DIR',str_replace('\\', '/', getcwd().'/'));
			//应用程序目录常量
			define('APP_DIR',ROOT_DIR.'App/');
			//框架目录常量
			define('FRAME_DIR',ROOT_DIR.'Frame/');
			//配置文件目录常量
			define('CONFIG_DIR',APP_DIR.'Config/');
			//配置数据库类文件目录常量
			define('DAO_DIR',FRAME_DIR.'dao/');
			//插件目录常量
			define('VENDOR_DIR',ROOT_DIR.'Vendor/');
			//Smarty目录常量
			define('SMARTY_DIR',VENDOR_DIR.'Smarty/');
			//public目录常量
			define('PUBLIC_DIR',ROOT_DIR.'Public/');
			//上传目录常量
			define('UPLOADS_DIR',ROOT_DIR.'Uploads/');
			
		}
		private static function initConfig()
		{
			$GLOBALS['conf']=include CONFIG_DIR.'conf.php';
		}
		private static function initDispatchParam()
		{
			$default_controller=$GLOBALS['conf']['App']['default_platform'];
			define('PlATFORM',isset($_GET['p'])?$_GET['p']:$default_controller);
			//确定平台后才能确定控制器,这里用PlATFORM
			$default_controller=$GLOBALS['conf'][PlATFORM]['default_controller'];
			define('CONTROLLER',isset($_GET['c'])?$_GET['c']:$default_controller);
			$default_action=$GLOBALS['conf'][PlATFORM]['default_action'];
			define('ACTION',isset($_GET['a'])?$_GET['a']:$default_action);
		}
		private static function initPlatformConst()
		{
			define('CURRENT_CON_DIR',APP_DIR.PlATFORM.'/Controller/');
			define('CURRENT_MODEL_DIR',APP_DIR.PlATFORM.'/Model/');
			define('CURRENT_VIEW_DIR',APP_DIR.PlATFORM.'/View/');
			define('CSS_DIR','/Public/'.PlATFORM.'/css/');
			define('JS_DIR','/Public/'.PlATFORM.'/js/');
			define('IMAGES_DIR','/Public/'.PlATFORM.'/images/');
		}
		public static function Autoload($class_name)
		{
			$frame_class_list=array
			(
				'Controller'=>FRAME_DIR.'Controller.class.php',
				'Model'=>FRAME_DIR.'Model.class.php',
				'Factory'=>FRAME_DIR.'Factory.class.php',
				'MySQLDB'=>DAO_DIR.'MySQLDB.class.php',
				'PDODB'=>DAO_DIR.'PDODB.class.php',
				'I_DAO'=>DAO_DIR.'I_DAO.interface.php',
				'Smarty'=>SMARTY_DIR.'Smarty.class.php',
				'Catpcha'=>VENDOR_DIR.'Catpcha.class.php',	
				'Upload'=>FRAME_DIR.'Upload.class.php',
				'Page'=>FRAME_DIR.'Page.class.php',
			);
			if(isset($frame_class_list[$class_name]))
			{
				include $frame_class_list[$class_name];
			}
			elseif(substr($class_name,-10) == 'Controller')
			{
				include CURRENT_CON_DIR.$class_name.'.class.php';
			}
			elseif(substr($class_name,-5) == 'Model')
			{
				include CURRENT_MODEL_DIR.$class_name.'.class.php';
			}
			
		}
		private static function initAutoload()
		{
			spl_autoload_register(array(__CLASS__,'Autoload'));
		}
		private static function initDispatch()
		{
			$controller_name=CONTROLLER.'Controller';
			$controller=new $controller_name;
			$action_name=ACTION.'Action';
			$controller->$action_name();
		}
	}